package com.blog.zip.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

public interface CategoryMapper {
    @Select("select c.*, GROUP_CONCAT(b3_solo_tag.tagTitle) AS tagTitle from b3_solo_category as c LEFT JOIN b3_solo_category_tag as t on\n" +
            "            c.oId = category_oId  left JOIN b3_solo_tag  on t.tag_oId = b3_solo_tag.oId GROUP BY c.oId")
    public List<Map<String, Object>> getCategory();

    @Select("select c.*, GROUP_CONCAT(b3_solo_tag.tagTitle) AS tagTitle from b3_solo_category as c LEFT JOIN b3_solo_category_tag as t on\n" +
            "            c.oId = category_oId  left JOIN b3_solo_tag  on t.tag_oId = b3_solo_tag.oId where c.categoryURI = #{type} GROUP BY c.oId ")
    public List<Map<String, Object>> getCategoryByType(String type);

    @Select("SELECT distinct art.oId,art.articleTitle,art.articlePermalink,art.articleCreated FROM `b3_solo_category` as c left join `b3_solo_category_tag` as " +
            " tag on c.oId = tag.category_oId LEFT JOIN \n" +
            " `b3_solo_tag_article` as a on tag.tag_oId = " +
            "a.tag_oId LEFT JOIN `b3_solo_article` as art  on a.article_oId =  art.oId  where c.oId = #{id} and art.articleStatus = 0 order by art.articleCreated desc")
    public List<Map<String, Object>> getCategoryArticle(String id);

    @Insert("insert into b3_solo_category (oId,categoryTitle,categoryURI,categoryOrder,categoryDescription,categoryTagCnt) values (" +
            "#{id},#{title},#{uri},#{order},#{desc},#{number})")
    public void addCategory(Map<String, Object> payload);

    @Insert("insert into b3_solo_category_tag (oId,category_oId,tag_oId) values (#{id},#{category},#{tagId})")
    public void addCategoryTag(Map<String, Object> map);

    @Update("update b3_solo_category set categoryTitle=#{title},categoryURI=#{uri},categoryOrder=#{order}," +
            "categoryDescription=#{desc},categoryTagCnt=#{number} where oId = #{id}")
    public void updateCategory(Map<String, Object> payload);

    @Update("update b3_solo_category_tag set tag_oId=#{tagId} where category_oId = #{category} ")
    public void updateCategoryTag(Map<String, Object> map);

    @Update("delete from b3_solo_category_tag where category_oId = #{category} ")
    public void deleteCategoryTag(String category);


    @Delete("delete from b3_solo_category where oId = #{oid}")
    public void deleteCategory(String oid);

}
